package com.report.repository;

import com.report.entity.AccountBookData;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface AccountBookDataRepository extends JpaRepository<AccountBookData, String> {

    Optional<AccountBookData> findByMenuAndSjxmbhAndAuditCompanyUsccAndTypeAndTableName(String menu, String sjxmbh, String auditCompanyUscc, Integer type, String tableName);

    default int upsert(AccountBookData accountBookData) {
        Optional<AccountBookData> existing = findByMenuAndSjxmbhAndAuditCompanyUsccAndTypeAndTableName(accountBookData.getMenu(), accountBookData.getSjxmbh(), accountBookData.getAuditCompanyUscc(), accountBookData.getType(), accountBookData.getTableName());
        if (!existing.isPresent()) {
            save(accountBookData);
        }
        return 1;
    }

}
